package com.greet.service.impl;

import com.greet.constant.MessageConstant;
import com.greet.context.BaseContext;
import com.greet.dto.CategoryDTO;
import com.greet.dto.CategoryPageQueryDTO;
import com.greet.entity.Category;
import com.greet.entity.Dish;
import com.greet.entity.Setmeal;
import com.greet.exception.DeletionNotAllowedException;
import com.greet.mapper.CategoryMapper;
import com.greet.mapper.DishMapper;
import com.greet.mapper.SetmealMapper;
import com.greet.result.PageResult;
import com.greet.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        categoryPageQueryDTO.setPage(
                (categoryPageQueryDTO.getPage() - 1) * categoryPageQueryDTO.getPageSize()
        );
        List<Category> categoryPage = categoryMapper.selectPageQuery(categoryPageQueryDTO);
        PageResult pageResult = new PageResult( categoryMapper.countAll(),
                                                categoryPage);
        return pageResult;
    }

    @Override
    public void setStatus(Integer status, Long id) {
        categoryMapper.update(
                Category.builder()
                        .status(status)
                        .id(id)
                        .updateTime(LocalDateTime.now())
                        .updateUser(BaseContext.getCurrentId())
                        .build()
        );
    }

    @Override
    public void insertCategory(CategoryDTO categoryDTO) {
        categoryMapper.insert(
                Category.builder()
                        .name(categoryDTO.getName())
                        .type(categoryDTO.getType())
                        .sort(categoryDTO.getSort())
                        .createTime(LocalDateTime.now())
                        .createUser(BaseContext.getCurrentId())
                        .updateTime(LocalDateTime.now())
                        .updateUser(BaseContext.getCurrentId())
                        .build()
        );
    }

    @Override
    public void deleteById(Long id) {
        List<Dish> selectDishes = dishMapper.select(
                Dish.builder().categoryId(id).build()
        );
        if(selectDishes.size() != 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> selectSetmeals = setmealMapper.select(
                Setmeal.builder().categoryId(id).build()
        );
        if(selectSetmeals.size() != 0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> selectByType(Integer type) {
        List<Category> res = categoryMapper.select(
                Category.builder().type(type).status(1).build()
        );
        return res;
    }
}
